package zhtt.common;

import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import zhtt.entity.Expression;
import zhtt.scheduler.ZhttScheduler;
import zhtt.service.ExpressionService;

import java.util.Date;

/**
 * Created by zhtt on 2016/10/28.
 * 项目启动的时候运行一些初始化工作
 */
@Service
public class InitService {

    @Autowired
    private ZhttScheduler zhttScheduler;

    @Autowired
    private ExpressionService expressionService;

    public void sync(){
        Expression expression=expressionService.getExpression();
        try {
            expression.setStartState(true);
            expression.setExpression("0 */1 * * * ?");//每隔1分钟执行一次
            expression.setExpression("*/5 * * * * ?");//每隔5秒执行一次
            expression.setRemarks("每隔5秒执行一次");
            zhttScheduler.init(new Date(), expression);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void destroy(){
        try {
            zhttScheduler.stop();
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
    }
}
